﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Dummies;
using Domain.Dummies.Dtos;
using Domain.Dummies.Dtos.Collections;
using AutoMapper;

namespace UniversalConsoleTester
{   

    class Program
    {
        static SessionDto session;        
        

        static void GetStudentsWithGroups()
        {
            StudentsWithGroupsDto target = new StudentsWithGroupsDto();
            var target_groups = new List<GroupWithStudentsDto>();
            target.Groups = target_groups;
            Mapper.CreateMap<GroupBaseDto, GroupWithStudentsDto>();


            DeaneryService.DeaneryServiceClient deaneryclient = new DeaneryService.DeaneryServiceClient();
            HrService.HrServiceClient hrclient = new HrService.HrServiceClient();

            deaneryclient.Open();
            hrclient.Open();

            GroupListDto grouplist = deaneryclient.GetGroups(session);
            IEnumerator<GroupBaseDto> it = grouplist.Groups.GetEnumerator();

            while (it.MoveNext())
            {
                var gws = Mapper.Map<GroupBaseDto, GroupWithStudentsDto>(it.Current);
                var gws_students = new List<StudentDto>();

                var students = deaneryclient.GetStudentsByGroup(session, it.Current.Id);
                IEnumerator<Guid> student_it = students.Guids.GetEnumerator();

                while (student_it.MoveNext())
                {
                    StudentDto student = hrclient.GetStudent(session, student_it.Current);
                    gws_students.Add(student);
                }
                gws.Students = gws_students;

                target_groups.Add(gws);
            }

            deaneryclient.Close();
            hrclient.Close();


            foreach (var group in target.Groups)
            {
                Console.WriteLine("Group id={0}, name={1}", group.Id, group.Name);

                foreach (var student in group.Students)
                    Console.WriteLine("  Student id={0}, name={1}", student.Id, student.Name);
            }
            
        }

        static void GetExamsByStudent()
        {
            Mapper.CreateMap<ExamDto, ExamWithSubjectAndTeacherDto>();
            ExamsListFullDataDto target = new ExamsListFullDataDto();
            List<ExamWithSubjectAndTeacherDto> EWSTLIST = new List<ExamWithSubjectAndTeacherDto>();
            target.Exams = EWSTLIST;
            
            DeaneryService.DeaneryServiceClient deaneryclient = new DeaneryService.DeaneryServiceClient();
            HrService.HrServiceClient hrclient = new HrService.HrServiceClient();
            DirectorateService.DirectorateServiceClient directorateclient = new DirectorateService.DirectorateServiceClient();

            deaneryclient.Open();
            hrclient.Open();
            directorateclient.Open();
                        
            ExamListDto examlist = deaneryclient.GetExamResults(session, new System.Guid(Database.TimurRachmatilaevGuidString));

            target.Student = hrclient.GetStudent(session, examlist.StudentGuid);
            IEnumerator<ExamDto> it_exam = examlist.Exams.GetEnumerator();
            while(it_exam.MoveNext())            
            {
                var EWST = Mapper.Map<ExamDto, ExamWithSubjectAndTeacherDto>(it_exam.Current);
                EWST.Subject = directorateclient.GetSubject(session, it_exam.Current.SubjectGuid);
                EWST.Teacher = hrclient.GetTeacher(session, it_exam.Current.TeacherGuid);
                EWSTLIST.Add(EWST);
            }

            deaneryclient.Close();
            hrclient.Close();
            directorateclient.Close();

            Console.WriteLine("StudentId={0} StudentName={1}", target.Student.Id, target.Student.Name);
            foreach (var exam in target.Exams)
            {
                Console.WriteLine(" {0} {1} {2} {3}",
                    exam.Id,
                    exam.Auditory,
                    exam.Date,
                    exam.Mark);
                Console.WriteLine("  Subject {0} {1} {2}",
                    exam.Subject.Id,
                    exam.Subject.Hours,
                    exam.Subject.Name);
                Console.WriteLine("  Teacher {0} {1}",
                    exam.Teacher.Id,
                    exam.Teacher.Name);

               }
        }

        static void GetTeacherWeekSchedule()
        {
            Mapper.CreateMap<TeacherLineScheduleDto, TeacherLineWSScheduleDto>();

            TeacherWeekFullDataScheduleDto week = new TeacherWeekFullDataScheduleDto();
            List<TeacherDayItemWSScheduleDto> days = new List<TeacherDayItemWSScheduleDto>();
            week.Days = days;

            ScheduleService.SchedulingServiceClient scheduleclient = new ScheduleService.SchedulingServiceClient();            
            scheduleclient.Open();

            TeacherWeekScheduleDto dockweek = scheduleclient.GetTeacherScheduleForAWeek(session,
                new System.Guid(Database.KorchomkinGuidString), System.DateTime.Now);

            scheduleclient.Close();

            HrService.HrServiceClient hrclient = new HrService.HrServiceClient();
            hrclient.Open();
            DirectorateService.DirectorateServiceClient directorateclient = new DirectorateService.DirectorateServiceClient();
            directorateclient.Open();
            

            if (dockweek != null && dockweek.Days != null)
            {
                IEnumerator<TeacherDayItemScheduleDto> dockdaysEnumerator = dockweek.Days.GetEnumerator();
                week.RequestedWeek = dockweek.RequestedWeek;
                week.Teacher = hrclient.GetTeacher(session, dockweek.TeacherGuid);

                while (dockdaysEnumerator.MoveNext())
                {
                    TeacherDayItemWSScheduleDto day = new TeacherDayItemWSScheduleDto();
                    days.Add(day);
                    List<TeacherLineWSScheduleDto> lines = new List<TeacherLineWSScheduleDto>();
                    day.WeekDay = dockdaysEnumerator.Current.WeekDay;
                    day.Lines = lines;                    

                    if (dockdaysEnumerator.Current.Lines != null)
                    {
                        IEnumerator<TeacherLineScheduleDto> docklinesEnumerator = dockdaysEnumerator.Current.Lines.GetEnumerator();
                        while (docklinesEnumerator.MoveNext())
                        {
                            TeacherLineWSScheduleDto line = Mapper.Map<TeacherLineScheduleDto, TeacherLineWSScheduleDto>(docklinesEnumerator.Current);
                            lines.Add(line);
                            line.Subject = directorateclient.GetSubject(session, docklinesEnumerator.Current.SubjectGuid);
                        }
                    }
                }
            }

            hrclient.Close();
            directorateclient.Close();

            TestClass.TestScheduleDatabaseFunctionality(week);            
        }

        static void GetTeacherDaySchedule()
        {
            Mapper.CreateMap<TeacherLineScheduleDto, TeacherLineWSScheduleDto>();

            TeacherDayFullDataScheduleDto day = new TeacherDayFullDataScheduleDto();
            DateTime targetday = DateTime.Parse("12.01.2011");
            List<TeacherLineWSScheduleDto> lines = new List<TeacherLineWSScheduleDto>();
            day.Lines = lines;
            day.RequestedDay = targetday;

            ScheduleService.SchedulingServiceClient scheduleclient = new ScheduleService.SchedulingServiceClient();
            scheduleclient.Open();
            TeacherDayScheduleDto dockday = scheduleclient.GetTeacherScheduleForADay(session, Guid.Empty, targetday);
            scheduleclient.Close();

            HrService.HrServiceClient hrclient = new HrService.HrServiceClient();
            hrclient.Open();
            DirectorateService.DirectorateServiceClient directorateclient = new DirectorateService.DirectorateServiceClient();
            directorateclient.Open();

            if (dockday != null && dockday.Lines != null)
            {
                day.Teacher = hrclient.GetTeacher(session, dockday.TeacherGuid);
                IEnumerator<TeacherLineScheduleDto> docklineEnumerator = dockday.Lines.GetEnumerator();

                while (docklineEnumerator.MoveNext())
                {
                    TeacherLineWSScheduleDto line = Mapper.Map<TeacherLineScheduleDto, TeacherLineWSScheduleDto>(docklineEnumerator.Current);
                    line.Subject = directorateclient.GetSubject(session, docklineEnumerator.Current.SubjectGuid);
                    lines.Add(line);
                }
            }

            hrclient.Close();
            directorateclient.Close();

            Console.WriteLine("=========TeacherDayFullDataScheduleDto==========");
            Console.WriteLine("At day {0} for teacher Id={1}, Name={2}",
                day.RequestedDay, day.Teacher.Id, day.Teacher.Name);

            if (day.Lines != null)
                foreach (TeacherLineWSScheduleDto line in day.Lines)
                {
                    Console.WriteLine("-----------------------------");
                    Console.WriteLine("in {0} at {1} will be subject Id={2}, Name={3}, Hour={4}",
                        line.Auditory, line.Time.TimeOfDay,
                        line.Subject.Id, line.Subject.Name, line.Subject.Hours);

                    if (line.GroupsGuids != null)
                        foreach (Guid groupguid in line.GroupsGuids)
                            Console.WriteLine("   group={0}", groupguid);
                }

        }

        static void GetWhoGotBook(Guid targetPerson)
        {
            Mapper.CreateMap<TeacherDto, PersonDto>();
            Mapper.CreateMap<StudentDto, PersonDto>();
            
            LibraryService.LibraryServiceClient libraryclient = new LibraryService.LibraryServiceClient();
            BooksListDto booklist = libraryclient.GetBooksGotByPerson(session, targetPerson);
            libraryclient.Close();

            BooksWithPersonListDto result = new BooksWithPersonListDto();
            result.Books = booklist.Books;

            HrService.HrServiceClient hrclient = new HrService.HrServiceClient();

            PersonDto person = hrclient.GetStudent(session, booklist.HolderPersonGuid);
            if (person.Id.Equals(Guid.Empty))
            {
                person = hrclient.GetTeacher(session, booklist.HolderPersonGuid);
                result.HolderPerson = Mapper.Map<TeacherDto, PersonDto>(person as TeacherDto);
            }
            else
                result.HolderPerson = Mapper.Map<StudentDto, PersonDto>(person as StudentDto);

            hrclient.Close();


            Console.WriteLine("WhoGotBookId={0}, Name={1}",
                result.HolderPerson.Id, result.HolderPerson.Name);
            foreach (var ibook in result.Books)
                Console.WriteLine("Id={0}, Author={1}, Title={2}, PagesCount={3}, Year={4}",
                    ibook.Id,
                    ibook.Author,
                    ibook.Title,
                    ibook.PagesCount,
                    ibook.Year);

            
        }

        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.Clear();

            session = new SessionDto();
            session.SessionGuid = Guid.Empty;
            session.__debug_string = "UniversalConsoleTester";

            //TestClass.TestMapper();
            //Program.GetStudentsWithGroups();
            //Program.GetExamsByStudent();

            //TestClass.TestScheduleDatabaseFunctionality(Database.KorchomkinWeekFullDataSchedule);
            //TestClass.TestScheduleServiceFunctionality();
            //Program.GetTeacherWeekSchedule();
            //TestClass.TestScheduleServiceDayFuntionality();
            //Program.GetTeacherDaySchedule();
            //TestClass.SearchBookByAuthorOrTitle();
            //TestClass.GetListOfBooksGotByPerson(new System.Guid(Database.KorchomkinGuidString));
            Program.GetWhoGotBook(new Guid(Database.KorchomkinGuidString));

            Console.ReadKey();
        }
    }
}
